package com.zyq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyq.annotation.Log;
import com.zyq.annotation.PreAuthorize;
import com.zyq.entity.SysRole;
import com.zyq.enuns.DeletedEnum;
import com.zyq.service.IRoleService;
import com.zyq.util.PageResult;
import com.zyq.util.R;
import com.zyq.vo.req.RolePageReqVo;
import com.zyq.vo.req.RoleReqVo;
import com.zyq.vo.req.RoleUpdateReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Set;

/**
 * 角色管理相关接口
 */
@RequestMapping("/roles")
@RestController
public class RoleController extends BaseController{

    @Autowired
    private IRoleService roleService;

    /**
     * 分页获取角色数据接口
     * @param reqVo
     * @return
     */
    @PreAuthorize(authorize = "sys:role:list")
    @Log(module = "组织管理-角色管理",action = "分页获取角色数据接口")
    @PostMapping("/page")
    public R<PageResult<SysRole>> pageInfo(@RequestBody RolePageReqVo reqVo){
        QueryWrapper<SysRole> query = Wrappers.query();
        query.lambda().eq(SysRole::getDeleted, DeletedEnum.UNDELETED.getCode());
        PageInfo<SysRole> pageInfo = PageHelper.startPage(reqVo.getPageNum(), reqVo.getPageSize()).doSelectPageInfo(() -> roleService.list(query));
        return R.ok(new PageResult<>(pageInfo));
    }

    /**
     * 新增角色
     * @param roleReqVo
     * @return
     */
    @PreAuthorize(authorize = "sys:role:add")
    @Log(module = "组织管理-角色管理",action = "新增角色接口")
    @PostMapping("/create")
    public R<String> createRole(@RequestBody @Valid RoleReqVo roleReqVo){
        return roleService.createRole(roleReqVo);
    }

    /**
     * 获取角色详情接口
     * @param roleId
     * @return
     */
    @PreAuthorize(authorize = "sys:role:detail")
    @Log(module = "组织管理-角色管理",action = "获取角色详情接口")
    @GetMapping("/{id}")
    public R<Set<String>> getRoleBy(@PathVariable("id") String roleId){
        return roleService.getRoleById(roleId);
    }

    /**
     * 更新角色信息
     * @param roleUpdateReqVo
     * @return
     */
    @PreAuthorize(authorize = "sys:role:update111 and sys:role:detail")
    @Log(module = "组织管理-角色管理",action = "更新角色信息接口")
    @PostMapping("/update")
    public R<String> updateRole(@RequestBody @Valid RoleUpdateReqVo roleUpdateReqVo){
        return roleService.updateRole(roleUpdateReqVo);
    }

    /**
     * 更新角色状态接口
     * @param roleId
     * @param status
     * @return
     */
    @PreAuthorize(authorize = "sys:role:update")
    @Log(module = "组织管理-角色管理",action = "更新角色状态接口")
    @PostMapping("/{id}/{status}")
    public R<String> updateRoleStatus(@PathVariable("id")String roleId,@PathVariable("status") Integer status){
        return roleService.updateRoleStatus(roleId,status);
    }

    /**
     * 删除角色接口
     * @param roleId
     * @return
     */
    @PreAuthorize(authorize = "sys:role:delete")
    @Log(module = "组织管理-角色管理",action = "删除角色接口")
    @PostMapping("/{id}")
    public R<String> deletedRole(@PathVariable("id") String roleId){
        return roleService.deletedRole(roleId);
    }
}
